from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from pathlib import Path
from app.database.database import create_tables
from app.routers import article, ai_model, prompt_template, settings, upload

app = FastAPI()

# 添加 CORS 中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有方法
    allow_headers=["*"],  # 允许所有头
)

# 创建数据库表
@app.on_event("startup")
async def startup():
    create_tables()

# 挂载路由
app.include_router(article.router, prefix="/api", tags=["articles"])
app.include_router(ai_model.router, prefix="/api", tags=["ai_models"])
app.include_router(prompt_template.router, prefix="/api", tags=["prompt_templates"])
app.include_router(settings.router, prefix="/api", tags=["settings"])
app.include_router(upload.router, prefix="/api", tags=["upload"])

# 添加静态文件服务
static_dir = Path(__file__).parent / "static"
app.mount("/api/static", StaticFiles(directory=str(static_dir)), name="static")

@app.get("/")
async def root():
    return {"message": "Welcome to AI Article Writer API"} 